﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Runtime.InteropServices;
namespace UG
{
    public static class GAC
    {
        
  [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("e707dcde-d1cd-11d2-bab9-00c04f8eceae")]
        internal interface IAssemblyCache
  {
    
    void UninstallAssembly(uint dwFlags, [MarshalAs(UnmanagedType.LPWStr)] string pszAssemblyName, IntPtr pvReserved, out uint    pulDisposition);
    
    void QueryAssemblyInfo(uint dwFlags, [MarshalAs(UnmanagedType.LPWStr)] string pszAssemblyName, IntPtr pAsmInfo);
    
    void CreateAssemblyCacheItem(uint dwFlags, IntPtr pvReserved, out IntPtr ppAsmItem, [MarshalAs(UnmanagedType.LPWStr)] String pszAssemblyName);
    
    void CreateAssemblyScavenger(out object ppAsmScavenger);
    
    void InstallAssembly(uint dwFlags, [MarshalAs(UnmanagedType.LPWStr)] string pszManifestFilePath, IntPtr pvReserved);
  }

        [DllImport("Fusion.dll", CharSet = CharSet.Auto)]
        internal static extern int CreateAssemblyCache(out IAssemblyCache ppAsmCache, uint dwReserved);

        
        
        public static bool UnInstallAssembly(string assemblyFullName)
        {
            IAssemblyCache asmc;
            int hr = CreateAssemblyCache(out asmc,0);
            if (hr == 0)
            {
                try
                {
                    uint dispo;
                    try
                    {                    
                        asmc.UninstallAssembly(0, assemblyFullName, IntPtr.Zero, out dispo);
                        return (dispo==1);
                    }
                    catch 
                    {
                        return false;
                    }
                }
                finally
                {
                    Marshal.ReleaseComObject(asmc);
                }
            }
            else
                return false;
        }

    };

 
}
